package com.example.demo.controller;


import com.example.demo.entity.Demand;
import com.example.demo.entity.User;
import com.example.demo.entity.dto.ResultBean;
import com.example.demo.exception.bizException.BizException;
import com.example.demo.exception.bizException.BizExceptionCodeEnum;
import com.example.demo.service.DemandService;
import com.example.demo.util.Role;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cy
 * @since 2020-08-01
 * @version v1.0
 */
@RestController
@RequestMapping("/demand")
public class DemandController {

    @Autowired
    private DemandService demandService;

    /**
    * 查询分页数据
    */
    @RequestMapping(method = RequestMethod.GET,value = "/normal")
    public ResultBean<?> listByPage(@RequestParam(name = "page", defaultValue = "1") int page,
                                    @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                    @RequestParam(name = "factor", defaultValue = "") String factor,
                                    @RequestParam(name = "demandType", defaultValue = "") String demandType,
                                    @RequestParam(name = "price", defaultValue = "false") boolean price,
                                    @RequestParam(name = "priceAsc", defaultValue = "true") boolean priceAsc,
                                    @RequestParam(name = "state", defaultValue = "") Integer state,
                                    @RequestParam(name = "uId", defaultValue = "") Integer uId,
                                    @RequestParam(name = "time", defaultValue = "true") boolean time,
                                    @RequestParam(name = "timeAsc", defaultValue = "false") boolean timeAsc,
                                    @RequestParam(name = "timeState", defaultValue = "") Integer timeState) {
        return new ResultBean<>(demandService.listDemandsByPage(page, pageSize,factor,demandType,price,priceAsc,state,uId,time,timeAsc,timeState));
    }


    /**
    * 根据id查询
    */
    @RequestMapping(method = RequestMethod.GET, value = "/normal/{id}")
    public ResultBean<?> getById(@PathVariable("id") Integer id) {
        return new ResultBean<>(demandService.getDemandById(id));
    }

    /**
    * 新增
    */
    @RequestMapping(method = RequestMethod.POST)
    @RequiresRoles({Role.FIRM})
    public ResultBean<?> insert(@RequestBody Demand demand) {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        demand.setUId(user.getUserId());
        return new ResultBean<>(demandService.insertDemand(demand));
    }

    /**
    * 删除
    */
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    @RequiresRoles({Role.FIRM})
    public ResultBean<?> deleteById(@PathVariable("id") Integer id) {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        return new ResultBean<>(demandService.deleteDemandById(id,user.getUserId()));
    }

    /**
    * 修改
    */
    @RequestMapping(method = RequestMethod.PUT)
    @RequiresRoles({Role.FIRM})
    public ResultBean<?> updateById(@RequestBody Demand demand) {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        demand.setUId(user.getUserId());
        return new ResultBean<>(demandService.updateDemand(demand));
    }

    @RequiresRoles(Role.ADMIN)
    @RequestMapping(method = RequestMethod.PUT, value = "/check")
    public ResultBean<?> checkDemand(@RequestBody Demand demand) {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        return new ResultBean<>(demandService.checkAchieve(demand));
    }

    /**
     * 导入需求
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(method = RequestMethod.POST,value = "/importDemand")
    public ResultBean<?> importDemand(@RequestParam("demand") MultipartFile file) throws IOException {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        String filename = file.getOriginalFilename();
        return new ResultBean<>(demandService.importDemand(filename,file,user.getUserId()));
    }

    /**
     *  查询未审核的需求数量
     */
    @RequestMapping(method = RequestMethod.GET,value = "/uncheckedNum")
    public  ResultBean<?> UncheckedNum(){
        return new ResultBean<>(demandService.UncheckedNum());
    }

}
